package com.youlai.boot.modules.content.service.impl;

import com.youlai.boot.core.security.util.SecurityUtils;
import com.youlai.boot.system.mapper.UserMapper;
import com.youlai.boot.system.model.bo.UserBO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.modules.content.mapper.LzCommentMapper;
import com.youlai.boot.modules.content.service.LzCommentService;
import com.youlai.boot.modules.content.model.entity.LzComment;
import com.youlai.boot.modules.content.model.form.LzCommentForm;
import com.youlai.boot.modules.content.model.query.LzCommentQuery;
import com.youlai.boot.modules.content.model.vo.LzCommentVO;
import com.youlai.boot.modules.content.converter.LzCommentConverter;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 评论服务实现类
 *
 * @author lz0816
 * @since 2025-03-06 16:49
 */
@Service
@RequiredArgsConstructor
public class LzCommentServiceImpl extends ServiceImpl<LzCommentMapper, LzComment> implements LzCommentService {

    private final LzCommentConverter lzCommentConverter;

    @Autowired
    private UserMapper userMapper;

    /**
     * 获取评论分页列表
     *
     * @param queryParams 查询参数
     * @return {@link IPage<LzCommentVO>} 评论分页列表
     */
    @Override
    public IPage<LzCommentVO> getLzCommentPage(LzCommentQuery queryParams) {
        Page<LzCommentVO> pageVO = this.baseMapper.getLzCommentPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }

    /**
     * 获取评论表单数据
     *
     * @param id 评论ID
     * @return
     */
    @Override
    public LzCommentForm getLzCommentFormData(Long id) {
        LzComment entity = this.getById(id);
        return lzCommentConverter.toForm(entity);
    }

    /**
     * 新增评论
     *
     * @param formData 评论表单对象
     * @return
     */
    @Override
    public boolean saveLzComment(LzCommentForm formData) {
        LzComment entity = lzCommentConverter.toEntity(formData);
        entity.setCreateBy(SecurityUtils.getUserId());
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.save(entity);
    }

    /**
     * 更新评论
     *
     * @param id       评论ID
     * @param formData 评论表单对象
     * @return
     */
    @Override
    public boolean updateLzComment(Long id, LzCommentForm formData) {
        LzComment entity = lzCommentConverter.toEntity(formData);
        entity.setUpdateBy(SecurityUtils.getUserId());
        return this.updateById(entity);
    }

    /**
     * 删除评论
     *
     * @param ids 评论ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteLzComments(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的评论数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

    @Override
    public IPage<LzCommentVO> getLzBlogCommentPage(LzCommentQuery queryParams) {
        List<LzComment> lzComments = this.baseMapper.selectList(null);
        // 获取全部所有评论
        Page<LzCommentVO> pageVO = this.baseMapper.getLzBlogCommentPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        for (LzCommentVO lzCommentVO : pageVO.getRecords()) {
            List<LzComment> children = lzComments.stream()
                    .filter(item -> item.getRootId().equals(lzCommentVO.getId()))
                    .collect(Collectors.toList());
            // 将children转换为Vo
            List<LzCommentVO> childrenVo = lzCommentConverter.toListVo(children);
            for (LzCommentVO commentVO : childrenVo) {
                UserBO userProfile = userMapper.getUserProfile(commentVO.getCreateBy());
                commentVO.setAvatar(userProfile.getAvatar());
                commentVO.setNickName(userProfile.getNickname());
                commentVO.setToCommentUserName(userMapper.getUserProfile(commentVO.getToCommentUserId()).getNickname());
            }
            lzCommentVO.setChildren(childrenVo);
            // 获取当前用户信息 并设置
            UserBO userProfile = userMapper.getUserProfile(lzCommentVO.getCreateBy());
            lzCommentVO.setAvatar(userProfile.getAvatar());
            lzCommentVO.setNickName(userProfile.getNickname());
        }

        return pageVO;
    }

}
